import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../common/models/ai/index.dart';
import '../../../../common/utils/log_util.dart';
import 'index.dart';

class AiSearchController extends GetxController {
  AiSearchController();

  final state = AiSearchState();

  final TextEditingController searchController = TextEditingController();

  final FocusNode searchNode = FocusNode();

  final List<AIClueInfo> clueListCache = [];

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    final map = Get.arguments;
    if (map != null) {
      clueListCache.clear();
      final list = map['clueList'] == null ? [] : map['clueList'] as List;
      clueListCache
          .addAll(list.map((group) => (group as AIClueInfo)).toList());
    }
    OLogger.d('clue list $clueListCache');
    super.onReady();
  }

  clickPickerClue(AIClueInfo clue) {
    Get.back(result: clue);
  }

  Future<void> onSearchClue(String key) async {
    state.searchClueList.clear();
    if (key.isNotEmpty) {
      List<AIClueInfo> list =
          clueListCache.where((a) => (a.title ?? '').contains(key)).toList();
      state.searchClueList.addAll(list);
    }
  }
}
